package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.annotation.CheckLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Autowired
	private VodService vodService;

	@Operation(summary = "查询当前用户所有专辑列表")
	@GetMapping(value = "/albumInfo/findUserAllAlbumList")
	public Result<List<AlbumInfo>> findUserAllAlbumList() {
		List<AlbumInfo> albumInfos = trackInfoService.findUserAllAlbumList();
		return Result.ok(albumInfos);
	}

	@Operation(summary = "上传声音")
	@PostMapping(value = "/trackInfo/uploadTrack")
	public Result<Map<String, String>> uploadTrack(MultipartFile file) {
		Map<String, String> map = vodService.uploadTrack(file);
		return Result.ok(map);
	}

	@Operation(summary = "保存声音信息")
	@PostMapping(value = "/trackInfo/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo) {
		trackInfoService.saveTrackInfo(trackInfoVo);
		return Result.ok();
	}

	@CheckLogin
	@Operation(summary = "获取当前用户声音分页列表")
	@PostMapping(value = "/trackInfo/findUserTrackPage/{pageNo}/{pageSize}")
	public Result<Page<TrackListVo>> findUserTrackPage(@PathVariable("pageNo") Integer pageNo,
													   @PathVariable("pageSize") Integer pageSize,
													   @RequestBody TrackInfoQuery trackInfoQuery) {

		Page<TrackListVo> page = new Page<>(pageNo, pageSize);
		page = trackInfoService.findUserTrackPage(page, trackInfoQuery);
		return Result.ok(page);
	}

	@Operation(summary = "查询声音信息")
	@GetMapping(value = "/trackInfo/getTrackInfo/{trackId}")
	public Result<TrackInfo> getTrackInfo(@PathVariable("trackId") Long trackId) {
		TrackInfo trackInfo = trackInfoService.getById(trackId);
		return Result.ok(trackInfo);
	}

	@Operation(summary = "修改声音信息")
	@PutMapping(value = "/trackInfo/updateTrackInfo/{trackId}")
	public Result updateTrackInfo(@PathVariable("trackId") Long trackId,
								  @RequestBody TrackInfo trackInfo) {
		if (Objects.isNull(trackInfo.getId()) && Objects.nonNull(trackId)) {
			trackInfo.setId(trackId);
		}
		trackInfoService.updateTrackInfo(trackInfo);
		return Result.ok();
	}

	@Operation(summary = "删除声音信息")
	@DeleteMapping(value = "/trackInfo/removeTrackInfo/{trackId}")
	public Result removeTrackInfo(@PathVariable("trackId") Long trackId) {
		trackInfoService.removeTrackInfo(trackId);
		return Result.ok();
	}

}

